package com.ejuornal.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.ejuornal.bean.Comment;
import com.ejuornal.bean.User;
import com.ejuornal.service.CommentService;
import com.ejuornal.service.UserService;
import com.ejuornal.springutil.SpringUtil;
import com.ejuornal.util.StrToIntArray;
import com.ejuornal.util.String2Ints;
import com.ejuornal.util.SubStringHTML;

public class ArticalComment extends BaseAction<Comment> {
	private static final long serialVersionUID = 1L;
	private CommentService commentService;
	private String articalId;
	private Comment commentInfo;
	private String commentId;
	private String perPeriodicalId;
	private String value;
	private Map<String, Object> dataMap;
	private List<Comment> comments;
	private String userName;
	
	public String findByDate() {
	    String beginDate = (String) super.getRequestParameter("beginDate");
	    String endDate = (String) super.getRequestParameter("endDate");
	    Date beginDate1 = null;
	    Date endDate2 = null;
	    try {
            beginDate1 = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
            endDate2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
        } catch (ParseException e) {
            return INPUT;
        }
	    if(endDate2.compareTo(beginDate1) > 0) {
	        String url = "admin/findByDate";
	        pages = commentService.findByDate((page - 1)*10, url, beginDate1, endDate2);
	        return SUCCESS;
	    } else {
	        return INPUT;
	    }
	   
	}
	
	
	public String getCommentByArticalId(){
		int articalIdInt = Integer.parseInt(articalId);
		commentService = (CommentService) SpringUtil.getBean("commentService");
		String url="admin/getCommentByArticalId";
		String type = "1";
		pages = commentService.findByTypeId((page - 1)*10, url, type, articalIdInt);
		return SUCCESS;
	}
	
	/**
	 * @Description 评论详情
	 * @author lwf
	 * @return
	 */
	public String commDetail(){
		UserService userService = (UserService) SpringUtil.getBean("userService");
		commentService = (CommentService) SpringUtil.getBean("commentService");
		int commentIdInt = Integer.parseInt(commentId);
		commentInfo = commentService.detailInfoComment(commentIdInt);
		int userId = commentInfo.getUserId();
		User user = userService.findById(userId);
		userName = user.getUserName();
		return "commDetail";
	}
	
	/**
	 * @Description 删除评论
	 * @author lwf
	 * @return
	 */
	public String deleteComment(){	
		commentService.delete(String2Ints.StringToInts(commentId));
		if("2".equals(value)) {
			return "success";
		}
		return "deleteComment";
	}
	
	/**
	 * 查询主编所管的期刊里所有文章的评论
	 * @author syb
	 * @return
	 */
	public String findByEditorChief() {
		comments = commentService.findByEditoryChiefId(super.getUserIdBySession());
		
		return "success";
	}
	
	/**
	 * 后台评论管理
	 * @author syb
	 * @return
	 */
	public String findAll() {
		String url = "admin/findAll";
		pages = commentService.findAll((page-1)*10, url);
		List<Comment> list = pages.getRecords();
		for (Comment comment : list) {
			String contentString = comment.getContent();
			String contentOver =SubStringHTML.subStringHTML(contentString, 40, "..");
			comment.setContent(contentOver);
			System.out.println(comment.getScore());
		}
		return "success";
	}
	
	/**
	 * 查询某期刊某期期刊的文章的评论
	 * @author syb
	 * @return
	 */
	public String findByPerPeriodicalId() {
		int perPeriodicalIdInt = Integer.parseInt(perPeriodicalId);
		int userId = super.getUserIdBySession();
		comments = commentService.findByPerPeriodicalId(userId, perPeriodicalIdInt);
		
		
		return "success";
	}
	
	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public String getPerPeriodicalId() {
		return perPeriodicalId;
	}

	public void setPerPeriodicalId(String perPeriodicalId) {
		this.perPeriodicalId = perPeriodicalId;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getArticalId() {
		
		return articalId;
	}

	public void setArticalId(String articalId) {
		this.articalId = articalId;
	}

	public CommentService getCommentService() {
		return commentService;
	}
	@Resource(name="commentService")
	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}

	public Comment getCommentInfo() {
		return commentInfo;
	}

	public void setCommentInfo(Comment commentInfo) {
		this.commentInfo = commentInfo;
	}

	public String getCommentId() {
		return commentId;
	}

	public void setCommentId(String commentId) {
		this.commentId = commentId;
	}


	public List<Comment> getComments() {
		return comments;
	}


	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}


	public String getUserName() {
		return userName;
	}


	public void setUserName(String userName) {
		this.userName = userName;
	}
	
}
